import Vue from "vue";
import App from "./App";
import router from "./router/router";
import store from "./vuex/index";
import Vuex from "vuex";

import VueWechatTitle from "vue-wechat-title";
import {systemInfo} from './common/common.js'

Vue.use(VueWechatTitle);

import loading from "./components/loading";//全局加载

//复制到粘贴板插件
import VueClipboard from "vue-clipboard2";

VueClipboard.config.autoSetContainer = true;
Vue.use(VueClipboard);

import AlertTip from "./components/alertTip.vue";
import VueCookie from "vue-cookie";
import md5 from "js-md5";
import "swiper/dist/css/swiper.css";

// 按需引入部分组件  vant

import { Button , List , Col , Row , Lazyload , PullRefresh , Cell , DatetimePicker , Popup , Toast ,DropdownMenu, DropdownItem, NavBar,Icon,ActionSheet,Field,Area,Picker,search} from 'vant';
Vue.use(Button).use(List).use(Col).use(Row).use(Lazyload).use(PullRefresh).use(Cell).use(DatetimePicker).use(Popup)
  .use(Toast).use(DropdownMenu).use(DropdownItem).use(NavBar).use(Icon).use(ActionSheet).use(Field).use(Area).use(Picker).use(search);
import { setStore, getStore, removeStore,setsesStore,getsesStore,removesesStore, getRandom } from "./commonUtil/mUtils";
import { formatDate } from "./commonUtil/formatDate";

// 视频
import VideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
Vue.use(VideoPlayer);

Vue.use(Vuex);
Vue.use(VueCookie);//cookie存储；

Vue.prototype.$loading = loading;//全局loading；

Vue.prototype.setStore = setStore;//全局本地存储数据；
Vue.prototype.getStore = getStore;//全局获取本地数据；
Vue.prototype.removeStore = removeStore;//全局移除本地数据；
Vue.prototype.setsesStore = setsesStore;//全局零时存储数据；
Vue.prototype.getsesStore = getsesStore;//全局获取零时本地数据；
Vue.prototype.removesesStore = removesesStore;//全局移除零时本地数据；
Vue.prototype.formatDate = formatDate;//全局时间戳转换；
Vue.prototype.$md5 = md5;
Vue.prototype.$systemInfo = systemInfo;
//弹出框禁止滑动
Vue.prototype.stopScroll = function () {
  var mo = function (e) { e.preventDefault() }
  document.body.style.overflow = 'hidden'
  document.addEventListener('touchmove', mo, false)// 禁止页面滑动
}

//弹出框可以滑动
Vue.prototype.canScroll = function () {
  var mo = function (e) {
    e.preventDefault()
  }
  document.body.style.overflow = ''// 出现滚动条
  document.removeEventListener('touchmove', mo, false)
}


// this.formatDate = this.formatDate(new Date(时间戳), 'MM-dd hh:mm')
//开启debug模式
Vue.config.debug = true;
Vue.config.productionTip = false;
//自定义全局公共弹窗;
Object.defineProperty(Vue.prototype, "$alert", {
  get() {
    let div = document.createElement("div");
    document.body.appendChild(div);
    return (message, close, confirm) => {
      const Constructor = Vue.extend(AlertTip);
      let Instance = new Constructor({
        data() {
          return {
            show: true,
            alertText: message,
            closeState: close && confirm ? true : false,
            confirmState: true
          };
        },
        methods: {
          // close: close ? close : function() {},
          // confirm: confirm ? confirm : function() {}
          close: close && confirm ? close : close,
          confirm: confirm ? confirm : close

        }
      }).$mount(div);
    };
  },
  set() {
  }
});

//当进入一个页面时会触发导航守卫 router.beforeEach 事件
//全局页面跳转是否启用loading
const routerLoading = true;
router.beforeEach((to, from, next) => {
  //routerLoading ? loading() : '' //如果全局启用页面跳转则加载loading
  if (to.matched.length === 0) {
    //如果上级能匹配到则转上级路由;如果上级也未匹配到路由则跳转重定向页面;
    from.name ? next({ name: from.name }) : next("/");
  } else {
    //如果匹配到正确跳转
    next();
  }
  if (to.meta.query && !to.query[to.meta.query]) {
    next("/");
  }
  const title = to.meta && to.meta.title;
  if(title){
    document.title = title;
  }
  next();
});

//管道符截取小数点后两位；
Vue.filter("priceFilter", function(value = 0) {
  return Number(value).toFixed(2);
});
//管道符截取小数点整数位；
Vue.filter("priceIntFilter", function(value = 0) {
  var newValue = Number(value).toFixed(2)
  return String(newValue).split(".")[0]
});
//管道符截取小数点小数位；
Vue.filter("priceFloatFilter", function(value = 0) {
  var newValue = Number(value).toFixed(2)
  return String(newValue).split(".")[1]
});

//自动登录或者去登录；

new Vue({
  el: "#app",
  router,
  store,
  components: { App },
  template: "<App/>"
});
